package com.cz.zslu.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.cz.zslu.common.ReturnObject;
import com.cz.zslu.entity.*;
import com.cz.zslu.interceptor.BaseContextHandler;
import com.cz.zslu.model.Follow;
import com.cz.zslu.model.Friend;
import com.cz.zslu.rest.BaseController;
import com.cz.zslu.service.*;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 关注店铺 前端控制器
 * </p>
 *
 * @author 森森
 * @since 2018-10-25
 */
@RestController
@RequestMapping("/sys/follow")
@Api(description = "关注店铺管理")
public class FollowController extends BaseController<IZslFollowService, ZslFollow> {
    @Autowired
    private IZslStoreCommodityService iZslStoreCommodityService;
    @Autowired
    private IZslStoreService iZslStoreService;
    @Autowired
    private ISysUserService iSysUserService;
    @Autowired
    private IBaseAreaService iBaseAreaService;
    @Autowired
    private IZslAuthenticationService iZslAuthenticationService;
    @Autowired
    private CommondityStatisticsService commondityStatisticsService;
    @Autowired
    private CompanyContactService companyContactService;


    @ApiOperation("新增关注商品")
    @RequestMapping(value = "/followCommodty", method = RequestMethod.POST)
    public ReturnObject<List<ZslFollow>> followCommodty(@ApiParam("商品id") Long commodtyId) {
        ZslFollow info = new ZslFollow();
        info.setUserId(BaseContextHandler.getUserID());
        info.setCommodtyId(commodtyId);
        ZslFollow yetFollow=baseService.selectOne(new EntityWrapper<ZslFollow>().eq("user_id",BaseContextHandler.getUserID()).eq("commodty_id",commodtyId));
        if(yetFollow==null){

            baseService.insert(info);
            return new ReturnObject<>(0, "关注成功", null);
        }else{
            return new ReturnObject<>(0, "已关注此商品！");
        }
    }

    @ApiOperation("取消关注商品")
    @RequestMapping(value = "/delFollowCommodty", method = RequestMethod.POST)
    public ReturnObject<List<ZslFollow>> delFollowCommodty(@ApiParam("商品id") Long commodtyId) {
        baseService.delete(new EntityWrapper<ZslFollow>().eq("commodty_id", commodtyId).eq("user_id",
                BaseContextHandler.getUserID()));
        return new ReturnObject<>(0, "取消成功", null);
    }

    @ApiOperation("批量取消关注商品")
    @RequestMapping(value = "/delFollowCommodtyByiIds", method = RequestMethod.POST)
    public ReturnObject<List<ZslFollow>> delFollowCommodtyByiIds(@ApiParam("商品id") Long[] id) {
        baseService.delete(new EntityWrapper<ZslFollow>().in("commodty_id", Arrays.asList(id)).eq("user_id",
                BaseContextHandler.getUserID()));
        return new ReturnObject<>(0, "取消成功", null);
    }

    @ApiOperation("获取当前用户关注商品分页")
    @RequestMapping(value = "/getMyFollowCommodtyPage", method = RequestMethod.POST)
    public ReturnObject<List<ZslStoreCommodity>> getMyFollowCommodtyPage(@ApiParam("每页条数") @RequestParam(defaultValue = "10") int limit,
                                                                         @ApiParam("页数") @RequestParam(defaultValue =
                                                                                 "1") int offset) {
        List<ZslStoreCommodity> list = Lists.newArrayList();
        EntityWrapper<ZslFollow> ew = new EntityWrapper<>();
        ew.eq("info.user_id", BaseContextHandler.getUserID());
        ew.isNotNull("info.commodty_id");
        ew.orderBy("info.crt_time", false);
        Page<ZslFollow> obj = baseService.selectPage(new Page<>(offset, limit), ew);
        for (ZslFollow info : obj.getRecords()) {
            list.add(iZslStoreCommodityService.selectById(info.getCommodtyId()));
        }
        return new ReturnObject<>(0, "获取成功", obj.getTotal(), obj.getPages(), list);
    }

    @ApiOperation("新增关注店铺")
    @RequestMapping(value = "/saveFollow", method = RequestMethod.POST)
    public ReturnObject<List<ZslFollow>> saveFollow(@ApiParam("商铺id") Long storeId) {
        ZslFollow info = new ZslFollow();
        info.setUserId(BaseContextHandler.getUserID());
        info.setStoreId(storeId);
        ZslStore zslStore = iZslStoreService.selectById(storeId);
        info.setStorename(zslStore.getName());
        ZslFollow yetFollow = baseService.selectOne(new EntityWrapper<ZslFollow>().eq("store_id", storeId).eq("user_id", BaseContextHandler.getUserID()));
        if(yetFollow!=null){
            return new ReturnObject<>(1, "您已关注此店铺！");
        }else{

            baseService.insert(info);
            return new ReturnObject<>(0, "关注成功", null);
        }

    }

    @ApiOperation("取消关注店铺")
    @RequestMapping(value = "/delFollow", method = RequestMethod.POST)
    public ReturnObject<List<ZslFollow>> delFollow(@ApiParam("id") Long id) {
        baseService.delete(new EntityWrapper<ZslFollow>().eq("store_id", id).eq("user_id",
                BaseContextHandler.getUserID()));
        return new ReturnObject<>(0, "取消成功");
    }


    @ApiOperation("批量取消关注店铺")
    @RequestMapping(value = "/delFollowByiIds", method = RequestMethod.POST)
    public ReturnObject<List<ZslFollow>> delFollowByiIds(@ApiParam("ids") Long[] id) {
        baseService.delete(new EntityWrapper<ZslFollow>().in("id", Arrays.asList(id)));
        return new ReturnObject<>(0, "取消成功", null);
    }

    @ApiOperation("获取当前用户关注商铺分页")
    @RequestMapping(value = "/getMyFollowPage", method = RequestMethod.POST)
    public ReturnObject<List<ZslFollow>> getMyFollowPage(@ApiParam("每页条数") @RequestParam(defaultValue = "10") int limit,
                                                         @ApiParam("页数") @RequestParam(defaultValue = "1") int offset) {
//        List<Follow> list = Lists.newArrayList();
        EntityWrapper<ZslFollow> ew = new EntityWrapper<>();
        ew.eq("info.user_id", BaseContextHandler.getUserID());
        ew.isNotNull("info.store_id");
        ew.orderBy("info.crt_time", false);
        Page<ZslFollow> obj = baseService.selectPage(new Page<>(offset, limit), ew);
        for (ZslFollow info : obj.getRecords()) {
//            List<ZslStoreCommodity> zslStoreCommodityList = iZslStoreCommodityService.selectPage(new Page<>(1, 10),
//                    new EntityWrapper<ZslStoreCommodity>().eq("info.state", 0).eq("info.store_id", info.getStoreId()).orderBy(
//                            "info.volume", false)).getRecords();
//            ZslStore store = iZslStoreService.selectById(info.getStoreId());
//            store.setEntName(iSysUserService.selectOne(new EntityWrapper<SysUser>().eq("store_id", info.getStoreId())).getCompanyName());
            List<CompanyContact> companyContacts = new ArrayList<>();
            CompanyContact companyContact = new CompanyContact();
            companyContacts = companyContactService.selectList(new EntityWrapper<CompanyContact>().eq("store_id", info.getStoreId()));

            if (companyContacts != null && companyContacts.size() > 0) {
                companyContact = companyContacts.get(0);
                info.setQq(companyContact.getQq());
                info.setPhone(companyContact.getTelephone());
            }
//            list.add(new Follow(store, zslStoreCommodityList,companyContact,info));
            SysUser sysUser = iSysUserService.selectById(info.getUserId());
            info.setStorename(sysUser.getStoreName());
        }
        return new ReturnObject<>(0, "获取成功", obj.getTotal(), obj.getPages(), obj.getRecords());
    }


    @ApiOperation("获取全部关注商铺分页")
    @RequestMapping(value = "/getAllFollowPage", method = RequestMethod.POST)
    public ReturnObject<List<ZslFollow>> getAllFollowPage(@ApiParam("每页条数") @RequestParam(defaultValue = "10") int limit,
                                                          @ApiParam("页数") @RequestParam(defaultValue = "1") int offset) {
        EntityWrapper<ZslFollow> ew = new EntityWrapper<>();
        ew.isNotNull("commodty_id");
        ew.orderBy("crt_time", false);
        Page<ZslFollow> obj = baseService.selectPage(new Page<>(offset, limit), ew);
        return new ReturnObject<>(0, "获取成功", obj.getTotal(), obj.getPages(), obj.getRecords());
    }


    @ApiOperation("批量设置/取消成商友")
    @RequestMapping(value = "/setFriend", method = RequestMethod.POST)
    public ReturnObject setFriend(@ApiParam("设为商友，传值为1；取消成为商友，传值为0") @RequestParam Integer isFriend,
                                  @ApiParam("ids") @RequestParam Long[] ids) {
        List<ZslFollow> list = baseService.selectBatchIds(Arrays.asList(ids));
        for (ZslFollow info : list) {
            info.setIsFriend(isFriend);
            baseService.updateById(info);
        }
        return new ReturnObject<>(0, "设置成功");
    }


    @ApiOperation("查询商友列表")
    @RequestMapping(value = "/findFriends", method = RequestMethod.POST)
    public ReturnObject<List<ZslFollow>> findFriends() {
        List<ZslFollow> list = baseService.selectList(new EntityWrapper<ZslFollow>().eq("is_friend", 1).eq("user_id", BaseContextHandler.getUserID()).isNotNull("store_id"));
        return new ReturnObject<List<ZslFollow>>(0, "获取成功", list);
    }

    @ApiOperation("获取商友详情")
    @RequestMapping(value = "/getFriendDetail", method = RequestMethod.POST)
    public ReturnObject<Friend> getFriendDetail(@ApiParam("id") @RequestParam Long id) {

        ZslFollow zslFollow = baseService.selectById(id);
        Friend friend = new Friend();
        ZslAuthentication zslAuthentication = iZslAuthenticationService.selectOne(new EntityWrapper<ZslAuthentication>().eq("store_id", zslFollow.getStoreId()));
        if (zslAuthentication != null) {
            friend.setCompanyName(zslAuthentication.getCompanyName());
            if (zslAuthentication.getState() == 2) {
                friend.setIsAuthentication(1);
            } else {
                friend.setIsAuthentication(0);
            }
        }
        CommondityStatistics commondityStatistics = commondityStatisticsService.selectOne(new EntityWrapper<CommondityStatistics>().eq("store_id", zslFollow.getStoreId()));
        if (commondityStatistics == null) {
            commondityStatistics = new CommondityStatistics();
        }
        friend.setCommondityStatistics(commondityStatistics);

        return new ReturnObject<Friend>(0, "获取成功", friend);
    }


}

